package com.lsh.payment.service.common;

import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import com.lsh.payment.api.model.basevo.BaseResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.util.Date;

/**
 * Project Name: lsh-payment
 *
 * @author miaozhuang
 * @date 16/7/15
 * 北京链商电子商务有限公司
 * Package com.lsh.payment.service.common
 * desc: 请求头信息校验
 */
public class HttpRequestFilter implements ContainerRequestFilter {

    private static Logger logger = LoggerFactory.getLogger(HttpRequestFilter.class);

    private final static String NOTIFY = "notify";

    /**
     * 请求头信息校验
     *
     * @param context 请求上下文
     * @throws IOException 异常
     */
    @Override
    public void filter(ContainerRequestContext context) throws IOException {
        MultivaluedMap<String, String> map = context.getHeaders();

        if (context.getUriInfo().getPath().contains(NOTIFY)) {
            logger.info("context.getUriInfo().getPath() is : " + context.getUriInfo().getPath());
            return;
        }

        String apiVersion = map.getFirst("api-version");
        String random = map.getFirst("sign");
        String platform = map.getFirst("platform");

        logger.info("api-version:sign:platform = " + apiVersion + ":" + random + ":" + platform);

//        if (StringUtils.isAnyBlank(apiversion, random, platform)) {
//            context.abortWith(bulidHeaderCheckNotPassResponse());
//        }
    }

    /**
     * 异常信息组装
     *
     * @return Response
     */
    private Response bulidHeaderCheckNotPassResponse() {

        return Response.ok().status(Response.Status.OK)
                .entity(new BaseResponse(1001001, "头信息参数错误", new Date()))
                .type(ContentType.APPLICATION_JSON_UTF_8)
                .header("X-Frame-Options", "SAMEORIGIN")
                .build();
    }
}
